<template>
  <div>
    <el-dialog :title="!dataForm.id ? '试戴片新增' : (type == 'update') ? '试戴片修改' : '试戴片详细'" :close-on-click-modal="false"
      :visible.sync="visible" :before-close="dialogCloseBefore" fullscreen center>
      <el-button class="miui-dialog-close-btn" @click="dialogCloseBefore">关闭页面</el-button>
      <el-divider content-position="left">必填项</el-divider>
      <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="制造商" prop="supplyName">
              <el-input v-model="dataForm.supplyName" readonly="readonly" :disabled="this.type != 'add'">
                <el-button slot="append" @click="getSupplyList">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品品种" prop="brandName">
              <el-input v-model="dataForm.brandName" readonly="readonly" :disabled="this.type != 'add'">
                <el-button slot="append" @click="getBrandList">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="dataForm.name" :disabled="this.type == 'check'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="球镜" prop="mirrorBall">
              <el-select v-model="dataForm.mirrorBall" :disabled="this.type != 'add'" clearable>
                <el-option v-for="item in mirrorBallOptions" :value='item.value' :label="item.label" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="柱镜" prop="columnMirror">
              <el-select v-model="dataForm.columnMirror" :disabled="this.type != 'add'" clearable>
                <el-option v-for="item in columnMirrorOptions" :value='item.value' :label="item.label" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品编号" prop="color" :disabled="this.type != 'add'">
              <el-input v-model="dataForm.color"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="轴向" prop="axialDirection">
              <el-input v-model="dataForm.axialDirection" :disabled="this.type != 'add'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="曲率" prop="curvature">
              <el-input v-model="dataForm.curvature"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="直径" prop="diameter">
              <el-input v-model="dataForm.diameter"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="计量单位" prop="unitType">
              <el-select v-model="dataForm.unitType" clearable>
                <el-option v-for="item in unitTypeData" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收费项目" prop="payType">
              <el-select v-model="dataForm.payType" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.payType" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="批号管理" prop="batchManagement">
              <el-radio-group v-model="dataForm.batchManagement">
                <el-radio :label="1" border>是</el-radio>
                <el-radio :label="0" border>否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品代码" prop="code">
              <el-input v-model="dataForm.code" readonly="readonly"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品条码" prop="barcode">
              <el-input v-model="dataForm.barcode" readonly="readonly"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8"></el-col>
        </el-row>

        <el-divider content-position="left">非必填项</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="使用类型" prop="useType">
              <el-select v-model="dataForm.useType" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.useType" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="抛弃型分类" prop="disusedClassification">
              <el-select v-model="dataForm.disusedClassification" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.disusedClassification" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品可使用天数" prop="useNumberDays">
              <el-input v-model="dataForm.useNumberDays"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="颜色" prop="chineseColor">
              <el-select v-model="dataForm.chineseColor" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.chineseColorData" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="效期提醒上限" prop="disusedClassification">
              <el-input v-model="dataForm.expiryReminderBefore"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="效期提醒下限" prop="useNumberDays">
              <el-input v-model="dataForm.expiryReminderEnd"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="隐形类别" prop="stealthType">
              <el-select v-model="dataForm.stealthType" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.stealthTypeOne" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="材质" prop="nursingLiquidType">
              <el-select v-model="dataForm.nursingLiquidType" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.nursingLiquidTypeOne" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
          	<el-form-item label="生产厂家" prop="manufacturerName">
          	  <el-input v-model="dataForm.manufacturerName">
          			<el-button slot="append" @click="openDialog('manufactor')">选择</el-button>
          		</el-input>
          	</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
          	<el-form-item label="供货商" prop="supplierName">
          	  <el-input v-model="dataForm.supplierName">
          			<el-button slot="append" @click="openDialog('supplier')">选择</el-button>
          		</el-input>
          	</el-form-item>
          </el-col>
          <el-col :span="8">
          	<el-form-item label="注册证号" prop="registerNum">
          	  <el-input v-model="dataForm.registerNum"></el-input>
          	</el-form-item>
          </el-col>
        </el-row>
        <goods-price :list="dataForm.list" :priceGroup="priceGroup"></goods-price>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
      </span>
      <select-supply v-if="selectSupplyVisible" ref="selectSupply" @refreshData="updateFormSupply"></select-supply>
      <select-brand v-if="selectBrandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
      <search-manufactor v-if="manufactorVisible" ref="manufactorDialog" @refreshData="getManuctorData"></search-manufactor>
      <search-supplier v-if="supplierVisible" ref="supplierDialog" @refreshData="getSupplierData"></search-supplier>
    </el-dialog>
  </div>
</template>

<script>
  import { remote } from '@/api/admin/dict'
  import { goodsGetObj, goodsAddObj, goodsPutObj } from '@/api/eims/goods'
  import { filterParams } from '@/util/tools'
  import { goodsPriceList } from '@/api/eims/goods-price'
  import SelectSupply from '@/components/business/SearchSupply'
  import SelectBrand from '@/components/business/SearchBrand'
  import GoodsPrice from './price'
  import SearchManufactor from '@/components/business/SearchManufactor'
  import SearchSupplier from '@/components/business/SearchSupplier'

  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        type: '',
        bizData: {},
        priceGroup: [],
        unitTypeData: [],
        dataForm: {
          id: 0,
          ctgId: '14',
          ctgSaleId: '16',
          name: '',
          code: '14.00.00.CA0000000.0000',
          barcode: '140000CA00000000000',
          supplyId: '',
          supplyName: '',
          supplyCode: '',
          brandId: '',
          brandName: '',
          brandCode: '',
          mirrorBall: '',
          columnMirror: '',
          color: '',
          status: '',
          unitType: '',
          batchManagement: 0,
          diameter: '', //直径
          curvature: '', //曲率
          axialDirection: '', //轴向
          payType: '', //收费类型
          useType: '', //使用类型
          disusedClassification: '', //抛弃型类型
          useNumberDays: '', //产品可使用天数
          chineseColor: '', //颜色
          expiryReminderBefore: '', //效期提醒上限
          expiryReminderEnd: '', //效期提醒下限
          stealthType: '', //隐形类别
          nursingLiquidType: '', //材质
          list: [] //价格组
        },
        dataRule: {
          name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
          supplyName: [{required: true, message: '制造商不能为空', trigger: 'blur'}],
          brandName: [{required: true, message: '商品品种不能为空', trigger: 'blur'}],
          unitType: [{required: true, message: '计量单位不能为空', trigger: 'blur'}],
          mirrorBall: [{required: true, message: '球镜不能为空', trigger: 'blur'}],
          columnMirror: [{required: true, message: '柱镜不能为空', trigger: 'blur'}],
          color: [{required: true, message: '商品编号不能为空', trigger: 'blur'}]

        },
        selectSupplyVisible: false,
        selectBrandVisible: false,
        manufactorVisible: false,
        supplierVisible: false,
        mirrorBallOptions: [],
        columnMirrorOptions: []
      }
    },
    components: {
      SelectSupply,
      SelectBrand,
      GoodsPrice,
      SearchManufactor,
      SearchSupplier
    },
    methods: {
      init(data) {
        this.bizData = this.$store.getters.bizData
        remote('unit_type').then(result => {
          this.unitTypeData = result.data.data
        })
        this.type = data.type
        this.dataForm.id = data.id || 0
        this.dataForm.ctgId = data.ctgId
        this.visible = true
        this.priceGroup = []
        this.dataForm.list = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // 获取球镜和柱镜范围
          remote('mirror_ball_enum').then(result => {
            this.mirrorBallOptions = result.data.data
          })
          remote('column_mirror_enum').then(result => {
            this.columnMirrorOptions = result.data.data
          })
          this.dataForm.supplyId = ''
          this.dataForm.supplyCode = ''
          this.dataForm.brandId = ''
          this.dataForm.brandCode = ''
          if (this.dataForm.id) {
            this.getGoodsPriceList().then((result) => {
              goodsGetObj(this.dataForm.id).then(response => {
                if(response.data.code == '0'){
                  this.dataForm = response.data.data
                  if(this.dataForm.list.length){
                    this.priceGroup = this.dataForm.list
                  }
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            })
          }else{
            this.getGoodsPriceList().then((result) => {
              this.priceGroup = [...result]
              this.dataForm.list = [...result]
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.list = this.priceGroup
            if (this.dataForm.id) {
              goodsPutObj(filterParams(this.dataForm)).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              this.dataForm.status = '1'
              goodsAddObj(filterParams(this.dataForm)).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }
        })
      },
      dialogCloseBefore(done) {
        this.visible = false
        this.$emit('refreshDataList')
        // done()
      },
      // 打开弹窗
      openDialog(type){
        if(type == 'manufactor'){
          this.manufactorVisible = true
          this.$nextTick(() => {
            this.$refs.manufactorDialog.init()
          })
        }else if(type == 'supplier'){
          this.supplierVisible = true
          this.$nextTick(() => {
            this.$refs.supplierDialog.init()
          })
        }
      },
      // 获取商品制造商信息
      getSupplyList() {
        let data = {
          ctgId: this.dataForm.ctgId
        }
        this.selectSupplyVisible = true
        this.$nextTick(() => {
          this.$refs.selectSupply.init(data)
        })
      },
      // 更新表单制造商信息
      updateFormSupply(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
        this.dataForm.supplyCode = data.code
      },
      // 获取商品品种信息
      getBrandList() {
        if (!this.dataForm.supplyId) {
          this.$message.warning('请选择制造商')
          return false
        }
        let data = {
          ctgId: this.dataForm.ctgId,
          supplyId: this.dataForm.supplyId
        }
        this.selectBrandVisible = true
        this.$nextTick(() => {
          this.$refs.selectBrand.init(data)
        })
      },
      //  更新表单品种信息
      updateFormBrand(data) {
        this.$set(this.dataForm,'brandId',data.id)
        this.$set(this.dataForm,'brandName',data.name)
        this.$set(this.dataForm,'brandCode',data.code)
        this.$set(this.dataForm,'name',data.name)
        this.$set(this.dataForm,'unitType',data.unitType)
        this.$set(this.dataForm,'manufacturerId',data.manufacturerId)
        this.$set(this.dataForm,'manufacturerName',data.manufacturerName)
        this.$set(this.dataForm,'registerNum',data.registerNum)
      },
      //  查询商品价格组列表
      async getGoodsPriceList() {
        let list = []
        await goodsPriceList().then(response => {
          if(response.data.code == '0'){
            let data = response.data.data
            data.forEach(item => {
              let obj = {}
              obj.priceGroupId = item.id
              obj.priceGroupName = item.groupName
              obj.taxRate = item.taxRate || 13
              list.push(obj)
            })
          }else{
            this.$message.error(response.data.msg)
          }
          // this.priceGroup = [...list]
          // this.dataForm.list = [...list]
        })
        return list
      },
      // 获取生产厂家信息
      getManuctorData(data){
        this.$set(this.dataForm,'manufacturerId',data.id)
        this.$set(this.dataForm,'manufacturerName',data.name)
      },
      // 获取供货商信息
      getSupplierData(data){
        this.$set(this.dataForm,'supplierId',data.id)
        this.$set(this.dataForm,'supplierName',data.name)
        this.dataForm = {...this.dataForm}
      }
    },
    computed: {
      newSupply() {
        return this.dataForm.supplyCode
      },
      newBrand() {
        return this.dataForm.brandCode
      },
      newMirrorBall() {
        return this.dataForm.mirrorBall
      },
      newColumnMirror() {
        return this.dataForm.columnMirror
      },
      newProid() {
        return this.dataForm.color
      }
    },
    watch: {
      // 制造商
      newSupply(val) {
        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + val).slice(-2);
        let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2);
        let mirrorBallNum = (Math.sign(this.dataForm.mirrorBall) == 1) ? 'CB' + (Array(4).join(0) + Math.abs(this.dataForm
          .mirrorBall * 100)).slice(-4) : 'CA' + (Array(4).join(0) + Math.abs(this.dataForm.mirrorBall * 100)).slice(
          -4)
        let columnMirrorNum = (Array(3).join(0) + Math.abs(this.dataForm.columnMirror * 100)).slice(-3);
        let serialNum = (Array(4).join(0) + this.dataForm.color).slice(-4);

        //  计算商品编码 / 商品条码
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + mirrorBallNum + columnMirrorNum + '.' +
          serialNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + mirrorBallNum + columnMirrorNum + serialNum
      },
      // 商品品种
      newBrand(val) {
        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2);
        let brandNum = (Array(2).join(0) + val).slice(-2);
        let mirrorBallNum = (Math.sign(this.dataForm.mirrorBall) == 1) ? 'CB' + (Array(4).join(0) + Math.abs(this.dataForm
          .mirrorBall * 100)).slice(-4) : 'CA' + (Array(4).join(0) + Math.abs(this.dataForm.mirrorBall * 100)).slice(
          -4)
        let columnMirrorNum = (Array(3).join(0) + Math.abs(this.dataForm.columnMirror * 100)).slice(-3);
        let serialNum = (Array(4).join(0) + this.dataForm.color).slice(-4);

        // 计算商品条码 / 商品编码
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + mirrorBallNum + columnMirrorNum + '.' +
          serialNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + mirrorBallNum + columnMirrorNum + serialNum
      },
      // 球镜
      newMirrorBall(val) {
        let mirrorBallNum = (Math.sign(val) == 1) ? 'CB' + (Array(4).join(0) + Math.abs(val * 100)).slice(-4) : 'CA' +
          (Array(4).join(0) + Math.abs(val * 100)).slice(-4)

        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2);
        let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2);
        let columnMirrorNum = (Array(3).join(0) + Math.abs(this.dataForm.columnMirror * 100)).slice(-3);
        let serialNum = (Array(4).join(0) + this.dataForm.color).slice(-4);

        //  计算商品编码 / 商品条码
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + mirrorBallNum + columnMirrorNum + '.' +
          serialNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + mirrorBallNum + columnMirrorNum + serialNum
      },
      // 柱镜
      newColumnMirror(val) {
        let columnMirrorNum = (Array(3).join(0) + Math.abs(val * 100)).slice(-3);

        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2);
        let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2);
        let mirrorBallNum = (Math.sign(this.dataForm.mirrorBall) == 1) ? 'CB' + (Array(4).join(0) + Math.abs(this.dataForm
          .mirrorBall * 100)).slice(-4) : 'CA' + (Array(4).join(0) + Math.abs(this.dataForm.mirrorBall * 100)).slice(
          -4)
        let serialNum = (Array(4).join(0) + this.dataForm.color).slice(-4);

        //  计算商品编码 / 商品条码
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + mirrorBallNum + columnMirrorNum + '.' +
          serialNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + mirrorBallNum + columnMirrorNum + serialNum
      },
      //  商品编号
      newProid(val) {
        let serialNum = (Array(4).join(0) + val).slice(-4);

        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2);
        let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2);
        let mirrorBallNum = (Math.sign(this.dataForm.mirrorBall) == 1) ? 'CB' + (Array(4).join(0) + Math.abs(this.dataForm
          .mirrorBall * 100)).slice(-4) : 'CA' + (Array(4).join(0) + Math.abs(this.dataForm.mirrorBall * 100)).slice(
          -4)
        let columnMirrorNum = (Array(3).join(0) + Math.abs(this.dataForm.columnMirror * 100)).slice(-3);

        //  计算商品编码 / 商品条码
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + mirrorBallNum + columnMirrorNum + '.' +
          serialNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + mirrorBallNum + columnMirrorNum + serialNum
      }
    }
  }
</script>
